package com.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.model.User;

/**
 * Servlet Filter implementation class UserCheckFilter
 */
public class LoginCheckFilter extends AbstractFilter implements Filter {
	private static List<String> allowedURIs;

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		if (allowedURIs == null) {

			allowedURIs = new ArrayList<String>();
			allowedURIs.add(fConfig.getInitParameter("loginActionURI"));

			/**
			 * CSS
			 */
			allowedURIs.add("/Intranet/javax.faces.resource/main.css.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/primefaces.css.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/nivo-slider.css.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/reset.css.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/social-icons.css.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/style.css.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/styled-elements.css.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/superfish.css.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.css.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/poshytip-1.0/src/tip-twitter/tip-twitter.css.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/poshytip-1.0/src/tip-yellowsimple/tip-yellowsimple.css.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/prettyPhoto/css/prettyPhoto.css.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/theme.css.xhtml");

			/**
			 * JS
			 */
			allowedURIs
					.add("/Intranet/javax.faces.resource/primefaces.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/jquery/jquery.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/jquery-1.10.2.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/jquery-ui.js.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/easing.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/jquery.scrollTo-1.4.2-min.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/jquery.cycle.all.js.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/custom.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/jquery.isotope.min.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/nivo-slider/jquery.nivo.slider.js.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/tabs.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/prettyPhoto/js/jquery.prettyPhoto.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/superfish-1.4.8/js/hoverIntent.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/superfish-1.4.8/js/superfish.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/superfish-1.4.8/js/supersubs.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/poshytip-1.0/src/jquery.poshytip.min.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/tweet/jquery.tweet.js.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js.xhtml");

			/**
			 * IMAGES
			 */
			allowedURIs
					.add("/Intranet/javax.faces.resource/messages/messages.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/images/ui-icons_2e83ff_256x240.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/images/ui-icons_38667f_256x240.png.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/logo.png.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/menu-bg.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/menu-arrow.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/tabs-divider.png.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/search.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/portfolio-divider.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/shadow-project.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/portfolio-left-shadow.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/plus-sign.png.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/balloon.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/shadow-blog.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/post-divider.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/post-divider-inverted.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/footer-glow.png.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/footer-bullet.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/bottom-shadow.png.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/to-top.png.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/noise.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/Arquivos.jpg.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/Arq-282_150.jpg.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/Risco.jpg.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/Risc-282_150.jpg.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/ContasAPagar.jpg.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/ContAPag-282_150.jpg.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/Ouvidoria.jpg.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/Ouv-282_150.jpg.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/ProcapCred.jpg.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/Procap-282_150.jpg.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/slider-bg.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/slider-shadow.png.xhtml");
			allowedURIs.add("/Intranet/javax.faces.resource/loading.gif.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/slider-nav-buttons.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/slider-arrows.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/tabs.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/shadow-1-3.png.xhtml");
			allowedURIs
					.add("/Intranet/javax.faces.resource/acessoRestrito.png.xhtml");
		}
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession session = req.getSession();

		if (session.isNew()) {
			doLogin(request, response, req);
			return;
		}

		User user = (User) session.getAttribute("user");

		if (user == null && !allowedURIs.contains(req.getRequestURI())) {
			System.out.println(req.getRequestURI());
			doLogin(request, response, req);
			return;
		}
		chain.doFilter(request, response);
	}
}